package com.zmn.oms.task.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.oms.business.interfaces.messageV1.text.MessageSendTextService;
import com.zmn.oms.business.interfaces.work.tag.OrderTagBService;
import com.zmn.oms.common.constant.MessageConsts;
import com.zmn.oms.common.constant.OrderTagConsts;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.model.dto.order.pay.DuplicatePayRefundDTO;
import com.zmn.oms.model.dto.tag.OrderTagAddDTO;
import com.zmn.oms.model.entity.detail.OrderDetail;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.services.interfaces.detail.OrderDetailService;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.business.interfaces.pay.OrderPayBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;

/**
 * 类描述：工单重复支付处理
 *
 * @author liuying
 * @date 2021/12/01 15:35
 */
@AliyunMQListener(topic = "${spring.mq.topic.oms}")
@Component
@Slf4j
public class OrderPayDuplicateListener {

    @NacosValue("${enable.review.payDuplicate:true}")
    private Boolean payDuplicateReview;

    @Autowired
    private OrderWorkService orderWorkService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private OrderPayBService orderPayBService;
    @Autowired
    private OrderTagBService orderTagBService;
    @Autowired
    private MessageSendTextService messageSendTextService;

    /**
     * 工单重复支付
     *
     * @param message
     */
    @AliyunMQHandler(tags = OmsMqTagConsts.ZMN_TOPIC_OMS_ORDER_PAY_DUPLICATE_TAG)
    public void payDuplicate(String message) {
        try {
            log.info("工单重复支付处理[{}]", message);
            if (!Objects.equals(payDuplicateReview, true)) {
                return;
            }

            // 退款结果
            DuplicatePayRefundDTO duplicatePayRefundDTO = JSON.parseObject(message, DuplicatePayRefundDTO.class);

            // 重复支付发起钉钉退款审批
            OrderWork orderWork = orderWorkService.findOrderWorkByKey(duplicatePayRefundDTO.getOrderId(), duplicatePayRefundDTO.getWorkId());
            // 只有啄木鸟重复支付，需要钉钉审批退款
            if (!Objects.equals(orderWork.getPlatWork(), GlobalConsts.PLAT_MARK_ZMN)) {
                return;
            }

            // 添加标记
            OrderTagAddDTO orderTagAddDTO = new OrderTagAddDTO();
            orderTagAddDTO.setOrderId(duplicatePayRefundDTO.getOrderId());
            orderTagAddDTO.setWorkId(duplicatePayRefundDTO.getWorkId());
            orderTagAddDTO.setTagIdList(Lists.newArrayList(OrderTagConsts.TAG_ID_DUPLICATE_PAY));
            orderTagBService.addOrderTag(orderTagAddDTO);

            // 发送钉钉通知
            duplicatePayRefundDTO.setOrderWork(orderWork);
            boolean result = orderPayBService.duplicatePayRefundApprove(duplicatePayRefundDTO);

            // 钉钉消息发送成功，发送短信通知用户
            if (result) {
                OrderDetail orderDetail = orderDetailService.findOrderDetailByKey(duplicatePayRefundDTO.getOrderId());
                Map<String, String> params = Maps.newHashMap();
                params.put("order_id", String.valueOf(duplicatePayRefundDTO.getOrderId()));
                messageSendTextService.sendTextMessage(MessageConsts.ZMN_MSG_RULEID_PAY_DUPLICATE, orderDetail.getTelephone(), params);
            }
        } catch (Exception e) {
            log.error("钉钉审批退款失败：" + e.getMessage(), e);
        }
    }
}
